home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex6.c < prev    next >
C/C++ Source or Header  |  1995-04-23  |  3KB  |  70 lines

  1. #include <genstub.c>
  2.  
  3. // Windows message procedure.
  4. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  5. {
  6.      switch ( uMsg ) {
  7.          case WM_CREATE:
  8.              DefWindowProc( hWnd, uMsg, wParam, lParam );
  9.              // if child set child name.
  10.              if ( strstr( GetCommandLine( ), "CHILD" ) )
  11.                  SetWindowText( hWnd, "CHILD" );
  12.              else // position parent to left of child
  13.                  MoveWindow( hWnd, 0, 0, 200, 200, TRUE );
  14.          break;
  15.          case WM_COMMAND: // process menu items
  16.              switch ( LOWORD( wParam ) ) {
  17.                  case IDM_TEST: {
  18.                      HANDLE hAlias;
  19.                      STARTUPINFO si;
  20.                      PROCESS_INFORMATION pi;
  21.  
  22.                      ZeroMemory( &si, sizeof( STARTUPINFO ) );
  23.                      ZeroMemory( &pi, sizeof( PROCESS_INFORMATION ) );
  24.                      si.cb=sizeof( STARTUPINFO );
  25.                      si.dwFlags = STARTF_USESHOWWINDOW;
  26.                      si.wShowWindow = SW_HIDE;
  27.                      CreateProcess( "GENERIC.EXE", "CHILD", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
  28.                      if ( pi.hProcess ) { // If we can make the process.
  29.                          HWND hWndExternal;
  30.                          HANDLE hMutex = CreateMutex( NULL, FALSE, "TEST_MUTEX" );
  31.                          DuplicateHandle( GetCurrentProcess( ), hMutex,
  32.                                           pi.hProcess, &hAlias, SYNCHRONIZE, FALSE, 0 );
  33.                          // Let initialization of new process occur.
  34.                          WaitForInputIdle( GetCurrentProcess( ), INFINITE );
  35.                          // Get external window address.
  36.                          while ( ( hWndExternal = FindWindow( lpszAppName, "CHILD" ) )==0 );
  37.                          // move it over to left
  38.                          MoveWindow( hWndExternal, 200, 0, 200, 200, TRUE );
  39.                          ShowWindow( hWndExternal, SW_SHOWNORMAL );
  40.                          PostMessage( hWndExternal, WM_USER, ( WPARAM )hAlias, 0 );
  41.                          CloseHandle( hMutex );
  42.                          Sleep( 5000 );
  43.                          TerminateProcess( pi.hProcess, 0 );
  44.                      }
  45.  
  46.                  }
  47.                  break;
  48.                  case IDM_EXIT:
  49.                      DestroyWindow( hWnd );
  50.                  break;
  51.              }
  52.          break;
  53.          case WM_USER: {
  54.              HDC hDC = GetDC( hWnd );
  55.              TextOut( hDC, 0, 0, "Waiting for mutex", 17);
  56.              WaitForSingleObject( ( HANDLE )wParam, INFINITE ); // get mutex.
  57.              TextOut( hDC, 0, 20, "Got mutex", 9);
  58.              Sleep( 3000 );
  59.              TextOut( hDC, 0, 40, "Released mutex", 14);
  60.              ReleaseMutex( ( HANDLE )wParam );
  61.          }
  62.          break;
  63.          case WM_DESTROY:
  64.              PostQuitMessage( 0 );
  65.          break;
  66.          default:
  67.              return ( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  68.     }
  69.     return ( NULL );
  70. }